package com.cxb.serviceprice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cxb.internalcommon.constant.CommonStatusEnum;
import com.cxb.internalcommon.dto.PriceRule;
import com.cxb.internalcommon.dto.ResponseResult;
import com.cxb.internalcommon.request.ForecastPriceDTO;
import com.cxb.internalcommon.responese.DirectionResponse;
import com.cxb.internalcommon.responese.ForecastPriceResponse;
import com.cxb.internalcommon.util.BigDecimalUtils;
import com.cxb.serviceprice.mapper.PriceRuleMapper;
import com.cxb.serviceprice.remote.ServiceMapClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ForecastPriceService {

    @Autowired
    private ServiceMapClient serviceMapClient;

    @Autowired
    private PriceRuleMapper priceRuleMapper;
    public ResponseResult forecastPrice(String depLongitude,String depLatitude ,String destLongitude,
                                        String destLatitude,String cityCode,String vehicleType ) {

        log.info("出发地经度:" + depLongitude);
        log.info("出发地纬度:" + depLatitude);
        log.info("目的地经度:" + destLongitude);
        log.info("目的地纬度:" + destLatitude);

        log.info("调用地图服务,查询距离和时长");
        ForecastPriceDTO forecastPriceDTO = new ForecastPriceDTO();
        forecastPriceDTO.setDepLongitude(depLongitude);
        forecastPriceDTO.setDepLatitude(depLatitude);
        forecastPriceDTO.setDestLongitude(destLongitude);
        forecastPriceDTO.setDestLatitude(destLatitude);
        //调用远程service-map服务
        ResponseResult<DirectionResponse> direction = serviceMapClient.direction(forecastPriceDTO);
        Integer distance = direction.getData().getDistance();
        Integer duration = direction.getData().getDuration();
        log.info("距离:"+distance+"时长:"+duration);


        log.info("读取计价规则");
        //查询条件
        Map<String,Object> QueryMap = new HashMap<>();
        QueryMap.put("city_code",cityCode);
        QueryMap.put("vehicle_type",vehicleType);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("city_code",cityCode);
        queryWrapper.eq("vehicle_type",vehicleType);
        queryWrapper.orderByDesc("fare_version");
        //从数据库查询数据
        List<PriceRule> priceRules = priceRuleMapper.selectList(queryWrapper);
        if(priceRules.size()==0){
            return ResponseResult.fail(CommonStatusEnum.PRICE_RULE_EMPTY.getCode(),
                    CommonStatusEnum.PRICE_RULE_EMPTY.getValue());
        }
        PriceRule priceRule = priceRules.get(0);

        log.info("根据距离,时长和计价规则，计算价格");

        double price = getPrice(distance, duration, priceRule);

        ForecastPriceResponse forecastPriceResponse = new ForecastPriceResponse();
        forecastPriceResponse.setPrice(price);
        forecastPriceResponse.setCityCode(cityCode);
        forecastPriceResponse.setVehicleType(vehicleType);
        forecastPriceResponse.setFareVersion(priceRule.getFareVersion());
        forecastPriceResponse.setFareType(priceRule.getFareType());
        return ResponseResult.success(forecastPriceResponse);
    }


    /**
     * 根据距离和时长及计价规则计算价格
     * @param distance 距离
     * @param duration 时长
     * @param priceRule 计价规则
     * @return
     */
    private double getPrice(Integer distance,Integer duration,PriceRule priceRule){
        double price =0;
        //起步价
        double startFare = priceRule.getStartFare();
        price = BigDecimalUtils.add(price, startFare);

        //里程费
        double distanceMile = BigDecimalUtils.divide(distance,1000);
        //起步里程
        double startMile = (double)priceRule.getStartMile();
        double distanceSubtract = BigDecimalUtils.substract(distanceMile,startMile);
        //收费里程
        double mile =  distanceSubtract<0?0:distanceSubtract;


        //计程单价
        double unitPricePerMile = priceRule.getUnitPricePerMile();

        //里程价格
        double mileFare = BigDecimalUtils.multiply(mile,unitPricePerMile);
        price = BigDecimalUtils.add(price,mileFare);

        //时长费
        //时长的分钟数
        double timeMinute = BigDecimalUtils.divide(duration,60);
        //每分钟单价
        double unitPricePerMinute = priceRule.getUnitPricePerMinute();
        //时长费用
        double timeFare =   BigDecimalUtils.multiply(timeMinute,unitPricePerMinute);

        price =  BigDecimalUtils.add(price,timeFare);

        BigDecimal priceBigDecimal = BigDecimal.valueOf(price);
        priceBigDecimal = priceBigDecimal.setScale(2,BigDecimal.ROUND_UP);
        return priceBigDecimal.doubleValue();
    }
}
